package com.mzy.basics.utils.Date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Hashtable;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/3/10 12:43
 * @Version 1.0
 */
public class SecurityDate {

    /**
     * java.util.Date 是非线程安全的，所有的日期类都是可变的，这是Java日期类最大的问题之一。
     *  SimpleDateFormat格式化工具也是一样，阿里巴巴规约中建议将SimpleDateFormat放到ThreadLocal中。
     * 而java8中日期和时间基本都被设计final，final修饰的类，天然线程安全。
     */


    /**
     * JDK8 主要使用LocalDate 和LocalTime作为时间类， 并且通过static 和final等来保证线程安全
     *
     * @param args
     */

    public static void main(String[] args) {
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);//Asia/Shanghai

        ZoneOffset zoneOffset = ZoneOffset.ofHours(8);
        System.out.println(zoneOffset);//+08:00

        Instant instant = Instant.ofEpochSecond(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)));
        System.out.println(instant.getEpochSecond());//1605596559

        Duration duration = Duration.between(LocalDateTime.now(), LocalDateTime.now().plusHours(1));
        System.out.println(duration.getSeconds());//3600

        Period period = Period.between(LocalDate.now(), LocalDate.now().plusDays(1));
        System.out.println(period.getDays());//1

        LocalDate date = LocalDate.now();
        System.out.println(date);//2020-11-17

        LocalTime time = LocalTime.now();
        System.out.println("当前时间" + time);//15:02:39.067

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);//2020-11-17T15:02:39.06

        ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), zoneId);
        System.out.println(zonedDateTime);//2020-11-17T15:02:39.067+08:00[Asia/Shanghai]

        OffsetDateTime offsetDateTime = OffsetDateTime.of(LocalDateTime.now(), ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2020-11-17T15:02:39.068+08:00

        String format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(offsetDateTime);
        System.out.println(format);//2020-11-17 15:02:39

        TemporalAdjuster temporalAdjuster = TemporalAdjusters.firstDayOfMonth();
        System.out.println(temporalAdjuster.adjustInto(LocalDate.now()));//2020-11-01

        LocalDate now1 = LocalDate.now();
        System.out.println(now1);




    }
}
